package com.palfund.ui.recyclerview.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.palfund.ui.R;

import java.util.HashMap;
import java.util.HashSet;

/**
 * Created by clvc on 2017/8/7.
 * 真正的速度是看不见的 !
 * Today is today , we will go !
 * <p>
 * 实现单选,多选功能
 */
public class LinearAdapter extends RecyclerViewAdapter<HashMap<String, Object>> {
    // 实现单选/多选,存储选择的Item
    public static HashSet<Integer> mSet = new HashSet<>();
    // 单选
    private static View curView;

    public LinearAdapter(Context context) {
        super(context);
    }
    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("Adapter", "---onCreateViewHolder()--->");
        // 必须使用此方法填充布局,否则item的match_parent属性失效
        View view = mInflater.inflate(R.layout.item1_recycleview, parent, false);
        return new LinearViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewAdapter.RecyclerViewHolder holder, int position) {
        Log.i("Adapter", "---onBindViewHolder()--->" + position);
        HashMap<String, Object> hashMap = mList.get(position);
        String index = (String) hashMap.get("index");
        Bitmap bitmap = (Bitmap) hashMap.get("image");
        LinearViewHolder baseViewHolder = (LinearViewHolder) holder;
        baseViewHolder.mTextView.setText(index);
        baseViewHolder.mImageView.setImageBitmap(bitmap);
        if (mSet.contains(position)) {
            baseViewHolder.mTextView.setBackgroundColor(Color.BLACK);
        } else {
            baseViewHolder.mTextView.setBackgroundResource(R.color.colorAccent);
        }
    }

    class LinearViewHolder extends RecyclerViewHolder {
        private ImageView mImageView;
        private TextView mTextView;

        public LinearViewHolder(View view) {
            super(view);
            mImageView = (ImageView) itemView.findViewById(R.id.imageView_item1);
            mTextView = (TextView) itemView.findViewById(R.id.textView_item1);
            //mImageView.setOnClickListener(this);
            mTextView.setOnClickListener(this);
            Log.i("LinearViewHolder", "---LinearViewHolder()--->");
        }

        @Override
        public void onClick(View v) {
            super.onClick(v);
            //获取子控件在布局中显示的位置
            int layoutPosition = getLayoutPosition();
            //获取在适配器中的位置
            int adapterPosition = getAdapterPosition();
            // 实现单选/多选
            if (mSet.contains(adapterPosition)) {
                // 包含,表明第二次点击,移除
                boolean removed = mSet.remove(adapterPosition);
                v.setBackgroundResource(R.color.colorAccent);
                curView = null;
                Log.i("LinearViewHolder", "---removed--->" + removed);
            } else {
                if (curView != null) {
                    curView.setBackgroundResource(R.color.colorAccent);
                    mSet.clear();
                }
                // 不包含,第一次点击,添加
                boolean added = mSet.add(adapterPosition);
                v.setBackgroundColor(Color.BLACK);
                Log.i("LinearViewHolder", "---onClick()--->" + added);
                curView = v;
            }
            Log.i("LinearViewHolder", "---onClick()--->" + layoutPosition + "   " +
                    adapterPosition);
            mOnItemViewClickListener.onItemViewClick(v, adapterPosition, mList.get
                    (adapterPosition));
        }
    }
}